package com.lrkj.payfee.service.impl;

import com.lrkj.common.exception.BaseException;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.payfee.domain.AdvanceReceipt;
import com.lrkj.payfee.mapper.AdvanceReceiptMapper;
import com.lrkj.payfee.service.IAdvanceReceiptService;
import com.lrkj.payfee.service.IAgainstRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 预收单Service业务层处理
 *
 * @author yajun_ren
 * @date 2021-08-06
 */
@Service
public class AdvanceReceiptServiceImpl implements IAdvanceReceiptService {
    @Autowired
    private AdvanceReceiptMapper advanceReceiptMapper;

    @Autowired
    private IAgainstRecordService againstRecordService;


    /**
     * 查询预收单
     *
     * @param madvanceReceiptId 预收单ID
     * @return 预收单
     */
    @Override
    public AdvanceReceipt selectAdvanceReceiptById(String madvanceReceiptId) {
        return advanceReceiptMapper.selectAdvanceReceiptById(madvanceReceiptId);
    }

    /**
     * 查询预收单列表
     *
     * @param advanceReceipt 预收单
     * @return 预收单
     */
    @Override
    public List<AdvanceReceipt> selectAdvanceReceiptList(AdvanceReceipt advanceReceipt) {
        return advanceReceiptMapper.selectAdvanceReceiptList(advanceReceipt);
    }

    /**
     * 新增预收单
     *
     * @param advanceReceipt 预收单
     * @return 结果
     */
    @Override
    public int insertAdvanceReceipt(AdvanceReceipt advanceReceipt) {
        advanceReceipt.setCreateTime(DateUtils.getNowDate());
        return advanceReceiptMapper.insertAdvanceReceipt(advanceReceipt);
    }

    /**
     * 修改预收单
     *
     * @param advanceReceipt 预收单
     * @return 结果
     */
    @Override
    public int updateAdvanceReceipt(AdvanceReceipt advanceReceipt) {
        advanceReceipt.setUpdateTime(DateUtils.getNowDate());
        return advanceReceiptMapper.updateAdvanceReceipt(advanceReceipt);
    }

    /**
     * 批量删除预收单
     *
     * @param madvanceReceiptIds 需要删除的预收单ID
     * @return 结果
     */
    @Override
    public int deleteAdvanceReceiptByIds(String[] madvanceReceiptIds) {
        return advanceReceiptMapper.deleteAdvanceReceiptByIds(madvanceReceiptIds);
    }

    /**
     * 删除预收单信息
     *
     * @param madvanceReceiptId 预收单ID
     * @return 结果
     */
    @Override
    public int deleteAdvanceReceiptById(String madvanceReceiptId) {
        return advanceReceiptMapper.deleteAdvanceReceiptById(madvanceReceiptId);
    }

    @Override
    public int updateByBatch(List<AdvanceReceipt> batchBean) {
        List<AdvanceReceipt> saveBeans = new ArrayList<>();
        if (batchBean.size() > 500) {
            for (int i = 0; i < batchBean.size(); i++) {
                if ((i + 1) % 500 == 0) {
                    saveBeans.add(batchBean.get(i));
                    advanceReceiptMapper.updateByBatch(saveBeans);
                    saveBeans.clear();
                } else {
                    saveBeans.add(batchBean.get(i));
                }
            }
            if (null != saveBeans && saveBeans.size() > 0) {
                advanceReceiptMapper.updateByBatch(saveBeans);
            }
        } else {
            if (batchBean.size() > 0) {
                advanceReceiptMapper.updateByBatch(batchBean);
            }
        }
        return 1;
    }

    @Override
    public BigDecimal getAdvanceMoney(String rommIds) {
        return advanceReceiptMapper.getAdvanceMoney(rommIds);
    }

    @Override
    @Transactional
    public int advanceRefund(String[] madvanceReceiptIds) {
        AdvanceReceipt eg = new AdvanceReceipt();
        eg.getParams().put("madvanceReceiptIds", madvanceReceiptIds);
        List<AdvanceReceipt> advanceReceipts = advanceReceiptMapper.selectAdvanceReceiptList(eg);
        advanceReceipts.stream().forEach(x -> {
            if (x.getAmountRemaining().compareTo(BigDecimal.ZERO) > 0) {
                againstRecordService.createAgainstRecord(x.getMadvanceReceiptId(), "", "预收退款", "", x.getAmountRemaining());
                x.setAmountRemaining(new BigDecimal(0));
                x.setStatus("10003");
                x.setUpdateTime(DateUtils.getNowDate());
                x.setUpdateBy(SecurityUtils.getUsername());
                advanceReceiptMapper.updateAdvanceReceipt(x);
            } else {
                throw new BaseException("预收单[" + x.getNo() + "]没有剩余金额。");
            }
        });
        return 1;
    }

    @Override
    public BigDecimal getAmountRemaining(AdvanceReceipt advanceReceipt) {
        return advanceReceiptMapper.getAmountRemaining(advanceReceipt);
    }

    @Override
    public int deleteByBusinessIds(String[] businessIds) {
        return advanceReceiptMapper.deleteByBusinessIds(businessIds);
    }
}
